﻿using EA.BA;
using EA.Common;
using EA.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAClient
{
    public partial class Login : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {

        }

        public void Logout(HttpRequest Request, HttpResponse Response)
        {
            Session.RemoveAll();
            this.isLoad = true;
        }

        public void SignIn(HttpRequest Request, HttpResponse Response)
        {
            
            String username = Request["email"];
            String password = Request["password"];

            if (username == null || username.Equals(""))
            {
                this.isLoad = true;
                Attributes.Add("error","Email can be not empty");
                return;
            }
            else if (password == null || password.Equals(""))
            {
                this.isLoad = true;
                Attributes.Add("error","Password can be not empty");
                return;
            }

            CustomerBA customerBA = new CustomerBA();
            CustomerTO user = customerBA.GetCustomerByEmail(username);
            if (username.Equals(user.Email) && Md5.md5(password).Equals(user.Password))
            {
                Session["USER_ID"] = user.ID;
                Session["EMAIL"] = user.Email;
                Response.Redirect("/Default.aspx");
            }
            else
            {
                this.isLoad = true;
                Attributes.Add("error","Login failed");
            }
        }
    }
}